package com.tbb.app.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.BeanUtils;

/**
 * @author LiuSenLin
 * @date 2020/2/13 23:00
 */
public class BeanUtilExt extends BeanUtils {

    /**
     * 拷贝对象属性值到另一个对象并返回
     *
     * @param source
     * @param target
     * @param <K>
     * @return
     */
    public static <K> K copyBean(Object source, K target) {
        copyProperties(source, target);
        return target;
    }

    public static <K> K copyBeanSuper(Object source, Class<K> target) {
        String s = JSON.toJSONString(source);
        return JSON.parseObject(s, target);
    }

    /**
     * 带泛型的Bean转化
     *
     * @param source
     * @param type
     * @param <K>
     * @return
     */
    public static <K> K copyBeanGenerics(Object source, TypeReference<K> type) {
        String s = JSON.toJSONString(source);
        return JSON.parseObject(s, type);
    }
}
